const resetPasswordToast = Swal.mixin({
    toast: true,
    position: 'center',
    background: '#ffffe1',
    showConfirmButton: false,
    backdrop: false,
    timer: 3000
});

function resetPasswordOnFinishClick(target) {
    var password = $('#password-input').val();
    var password2 = $('#password-retype-input').val();
    var email = $('#email-input').val();

    if (password == '') {
        resetPasswordToast.fire({
            type: "error",
            title: "密码不能为空",
        });
        return;
    }

    if (password != password2) {
        resetPasswordToast.fire({
            type: "error",
            title: "输入的两次密码不一样",
        });
        return;
    }

    var str = JSON.stringify({"email": email, "password": password});
    var key = makeKey(16);
    var encrypted = CryptoJS.AES.encrypt(str, key).toString();

    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    key = encrypt.encrypt(key);

    $.ajax({
        type: 'post',
        async: true,
        data: {"account": encrypted, "key": key},
        url: document.location.origin + '/api/updatepasswordbyemail',
        dataType: 'text',
        error: function() {

        },
        success: function(data) {
            var json = JSON.parse(data);

            if (json.result == 200) {
                showDialogToJumpToPage('新密码设置成功', '/', '去主页');
                $(target).prop('disabled', true);
            }
            else {
                resetPasswordToast.fire({
                    type: 'error',
                    title: json.description,
                });
            }
        }
    });
}